﻿using liaowa2.Common;
using liaowa2.Entity.DataModel;
using liaowa2.Interface.Manager;
using System;
using System.Web.Mvc;

namespace liaowa2.Web.Controllers
{
    public class NavController : BaseController
    {
        INavigationInterface navigationAppService;

        public NavController(INavigationInterface navigationAppService)
        {
            this.navigationAppService = navigationAppService;
        }

        // GET: /Admin/Nav/
        public ActionResult List()
        {
            return View();
        }

        public ActionResult Edit(int? id, int? parent_id)
        {
            Navigation model = new Navigation();
            if (id.HasValue)
            {
                model = navigationAppService.GetModel(id ?? 0);
                if (model != null)
                {
                    return View(model);
                }
                else
                {
                    AlertMessage("获取信息失败");
                    return null;
                }
            }
            model.parent_id = parent_id ?? 0;
            return View(model);
        }

        [HttpPost]
        public ActionResult Update(FormCollection collection)
        {
            Navigation model = new Navigation();
            int id = Convert.ToInt16(collection["id"]);
            int txtSortId = Convert.ToInt16(collection["txtSortId"]);
            int is_lock = collection["cbIsLock"].Contains("true") ? 1 : 0;
            string txtTitle = collection["txtTitle"];
            string txtSubTitle = collection["txtSubTitle"];
            string txtLinkUrl = collection["txtLinkUrl"];
            string txtRemark = collection["txtRemark"];
            string txtName = collection["txtName"];
            string actionType = collection["cblActionType"];
            string icon_url = collection["txtIconUrl"];
            int PanretID = Convert.ToInt16(collection["PanretID"]);
            if (id > 0)
            {
                model = navigationAppService.GetModel(id);
                if (model != null)
                {
                    model.name = txtName;
                    model.sort_id = txtSortId;
                    model.is_lock = is_lock;
                    model.title = txtTitle;
                    model.sub_title = txtSubTitle;
                    model.link_url = txtLinkUrl;
                    model.remark = txtRemark;
                    model.is_sys = 1;
                    model.action_type = actionType;
                    model.icon_url = icon_url;
                    model.nav_type = DTEnums.NavigationEnum.System.ToString();
                    if (navigationAppService.Update(model))
                    {
                        AlertMessage("编辑成功");
                        return View();
                    }
                    else
                    {
                        AlertMessage("编辑失败");
                        return null;
                    }
                }
                else
                {
                    AlertMessage("获取信息失败");
                    return null;
                }
            }
            else
            {
                if (PanretID > 0)
                {
                    Navigation parentNav = navigationAppService.GetModel(PanretID);
                    if (parentNav != null)
                    {
                        model.class_layer = parentNav.class_layer + 1;
                    }
                }
                model.name = txtName;
                model.sort_id = txtSortId;
                model.is_lock = is_lock;
                model.title = txtTitle;
                model.sub_title = txtSubTitle;
                model.link_url = txtLinkUrl;
                model.remark = txtRemark;
                model.parent_id = PanretID;
                model.is_sys = 1;
                model.action_type = actionType;
                model.icon_url = icon_url;
                model.nav_type = DTEnums.NavigationEnum.System.ToString();
                if (navigationAppService.Add(model) > 0)
                {
                    AlertMessage("新增成功");
                    return View();
                }
                else
                {
                    AlertMessage("新增失败");
                    return View();
                }
            }
        }

        public JsonResult Del(string id)
        {
            if (id.Length > 0)
            {
                id = id.Remove(id.Length - 1, 1);
                if (navigationAppService.DeleteList(id))
                {
                    return Json(new { state = 1, msg = "" }, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    return Json(new { state = 0, msg = "删除失败" }, JsonRequestBehavior.AllowGet);
                }
            }
            return Json(new { state = 0, msg = "参数错误" }, JsonRequestBehavior.AllowGet);
        }
    }
}